<?php
/**
 * Created by PhpStorm.
 * User: fairy
 * Date: 14-3-5
 * Time: 上午10:42
 */

class CURDAction {
    public function readBySQL(){
        $stu=new Model("Student");

        $result=$stu->where('YEAR(birthday)>=1989')->order('birthday DESC')->limit(3)->select();
        echo '<table border="2">';
        echo <<<TITLE
        <tr>
        <td>姓名</td>
        <td>学号</td>
        <td>专业</td>
        <td>电话</td>
        <td>生日</td>
        <tr>
TITLE;
        foreach($result as $row){
            echo <<<TR
        <tr>
        <td>{$row["name"]}</td>
        <td>{$row["sid"]}</td>
        <td>{$row["major"]}</td>
        <td>{$row["tel"]}</td>
        <td>{$row["birthday"]}</td>
        <tr>
TR;

        }
        echo '</table>';

    }
    public function add(){
        $stu=M("Student");
        $data=array(
            'name'=>'peng',
        'sid'=>'12010054011',
            'major'=>'阴阳',
            'tel'=>'12010054010',
            'birthday'=>'1990-09-21'
        );
        if(!$stu->where($data)->select())
        {
            echo '不存在数据，执行插入数据\n';
            $stu->data($data)->add();
            echo '新插入的数据：';
            echo '<table border="2">';
            echo <<<TITLE
        <tr>
        <td>姓名</td>
        <td>学号</td>
        <td>专业</td>
        <td>电话</td>
        <td>生日</td>
        <tr>
TITLE;
            $result=$stu->where($data)->select();
            foreach($result as $row){
                echo <<<TR
        <tr>
        <td>{$row["name"]}</td>
        <td>{$row["sid"]}</td>
        <td>{$row["major"]}</td>
        <td>{$row["tel"]}</td>
        <td>{$row["birthday"]}</td>
        <tr>
TR;

            }
            echo '</table>';

        }else echo '已经存在数据了';

    }
    public function update(){
        $stu=M("Student");
        $before=$stu->where('sid=12010054000')->select();
        echo '更新前的数据';
        echo '<table border="2">';
        echo <<<TITLE
        <tr>
        <td>姓名</td>
        <td>学号</td>
        <td>专业</td>
        <td>电话</td>
        <td>生日</td>
        <tr>
TITLE;
        foreach($before as $row){
            echo <<<TR
        <tr>
        <td>{$row["name"]}</td>
        <td>{$row["sid"]}</td>
        <td>{$row["major"]}</td>
        <td>{$row["tel"]}</td>
        <td>{$row["birthday"]}</td>
        <tr>
TR;
        }
            echo '更新之后的数据';
            $newdata=array(
                'name'=>'peng',
                'sid'=>'12010054005',
                'major'=>'阴阳',
                'tel'=>'12010054010',
                'birthday'=>'1990-09-21'
            );
            $stu->where('sid=12010054000')->save($newdata);
            echo '<table border="2">';
            echo <<<TITLE
        <tr>
        <td>姓名</td>
        <td>学号</td>
        <td>专业</td>
        <td>电话</td>
        <td>生日</td>
        <tr>
TITLE;
            foreach($stu->where('sid=12010054005')->select() as $row){
                echo <<<TR
        <tr>
        <td>{$row["name"]}</td>
        <td>{$row["sid"]}</td>
        <td>{$row["major"]}</td>
        <td>{$row["tel"]}</td>
        <td>{$row["birthday"]}</td>
        <tr>
TR;
    }
}
    public function delete(){
        $stu=M('Student');
        $before=$stu->where('sid=12010054011')->select();
        echo '删除之前:<br>';
        echo '<table border="2">';
        echo <<<TITLE
        <tr>
        <td>姓名</td>
        <td>学号</td>
        <td>专业</td>
        <td>电话</td>
        <td>生日</td>
        <tr>
TITLE;
        foreach($before as $row){
            echo <<<TR
        <tr>
        <td>{$row["name"]}</td>
        <td>{$row["sid"]}</td>
        <td>{$row["major"]}</td>
        <td>{$row["tel"]}</td>
        <td>{$row["birthday"]}</td>
        <tr>
TR;
    }echo '</table>';
        echo '删除之后:<br>';
        $stu->where('sid=12010054011')->delete();
        $result=$stu->select();
        echo '<table border="2">';
        echo <<<TITLE
        <tr>
        <td>姓名</td>
        <td>学号</td>
        <td>专业</td>
        <td>电话</td>
        <td>生日</td>
        <tr>
TITLE;
        foreach($result as $row){
            echo <<<TR
        <tr>
        <td>{$row["name"]}</td>
        <td>{$row["sid"]}</td>
        <td>{$row["major"]}</td>
        <td>{$row["tel"]}</td>
        <td>{$row["birthday"]}</td>
        <tr>
TR;
        }echo '</table>';
}
}